package com.angel.provider.sys.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.angel.core.base.BaseProviderImpl;
import com.angel.dao.health.SysDictMapper;
import com.angel.health.SysDict;
import com.angel.provider.sys.sysManagerDictProvider;

/**
 * 高亚林
 * 2016年9月25日下午6:25:59
 */
@Service
public class SysManagerDictProviderImpl extends BaseProviderImpl<SysDict> implements sysManagerDictProvider{

	@Autowired
	private SysDictMapper mapper;
	
	//批量删除
	public int delete(String ids) {
		try {
			String[] idarr = ids.split("&");
			for (String id : idarr) {
				SysDict dict = new SysDict();
				dict.setId(Integer.parseInt(id));
				dict.setDelFlag("0");
				mapper.updateByPrimaryKeySelective(dict);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}

	//删除单个
	public int deleteById(SysDict dict) {
		dict.setDelFlag("0");
		return mapper.updateByPrimaryKeySelective(dict);
	}

	//保存
	public Map<String, String> save(SysDict dict) {
		Map<String, String> map = new HashMap<String, String>();
		if(dict == null || dict.getValue() == null || dict.getValue() == ""){
			map.put("msg", "键值不能为空");
			map.put("code", "0");
			return map;
		}
		SysDict sysDict = new SysDict();
		sysDict.setValue(dict.getValue());
		List<Map<String, Object>> list = mapper.findAll(sysDict);
		if(list != null && list.size() > 0){
			map.put("msg", "字典已存在");
			map.put("code", "0");
			return map;
		}
		dict.setCreateTime(new Date());
		dict.setDelFlag("1");
		mapper.insert(dict);
		map.put("msg", "添加成功");
		map.put("code", "1");
		return map;
	}

	//根据id查询
	public SysDict findUserById(SysDict dict) {
		if(dict != null && dict.getId() != null ){
			return mapper.selectByPrimaryKey(dict.getId());
		}else{
			return null;
		}
	}

	@Override
	public Map<String, String> update(SysDict dict) {
		Map<String, String> map = new HashMap<String, String>();
		if(dict == null || dict.getValue() == null || dict.getValue() == ""){
			map.put("msg", "键值不能为空");
			map.put("code", "0");
			return map;
		}
		dict.setUpdateTime(new Date());
		mapper.updateByPrimaryKeySelective(dict);
		map.put("msg", "修改成功");
		map.put("code", "1");
		return map;
	}

}
